package com.ybzl.gateway.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.ybzl.gateway.domain.*;
import com.ybzl.gateway.mapper.DeviceMapper;
import com.ybzl.gateway.service.IDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;


@Service
public class IDeviceServiceImpl implements IDeviceService {
    @Autowired
    private DeviceMapper deviceMapper;

    @Override
    public void insertHeartbeat(DeviceHeartbeatInfo deviceHeartbeatInfo) {
        deviceMapper.insertHeartbeat(deviceHeartbeatInfo);
    }

    @Override
    public void insertDeviceInfo(DeviceInfo deviceInfo) {
        deviceMapper.insertDeviceInfo(deviceInfo);
    }

    @Override
    public List<Map<String, Object>> selectDeviceInfoByMinute() {
        return deviceMapper.selectDeviceInfoByMinute();
    }

    @Override
    public List<String> selectHeartbeatByMinute() {
        return deviceMapper.selectHeartbeatByMinute();
    }

    @Override
    public void insertDeviceStatus(List<DeviceStatusInfo> statusList) {
        for (DeviceStatusInfo deviceStatusInfo : statusList) {
            deviceMapper.insertDeviceStatus(deviceStatusInfo);
        }
    }

    @Override
    public List<DeviceStatusInfo> selectDeviceStatusInfo() {
        return deviceMapper.selectDeviceStatusInfo();
    }

    @Override
    public List<DeviceInfo> selectDeviceInfoBySecond() {
        return deviceMapper.selectDeviceInfoBySecond();
    }

    @Override
    public DeviceInfo selectLastByTagId(String tagId) {
        return deviceMapper.selectLastByTagId(tagId);
    }

    @Override
    public void insertTargetDeviceInfo(DeviceInfo deviceInfo) {
        deviceMapper.insertTargetDeviceInfo(deviceInfo);
    }

    @Override
    public List<DeviceInfo> selectTargetDeviceInfoBySecond() {
        return deviceMapper.selectTargetDeviceInfoBySecond();
    }

    @DS("location_meta")
    @Override
    public void saveOrUpdate(String beaconId, Integer power, Double x, Double y) {
        // 先查询
        Beacon beacon = deviceMapper.selectBeaconById(beaconId);
        // 没有新增
        deviceMapper.insertBeacon(new Beacon(Objects.isNull(beacon) ? null : beacon.getTs(), beaconId, power, x, y));
    }

    @DS("location_meta")
    @Override
    public List<Beacon> selectBeacon() {
        return deviceMapper.selectBeacon();
    }

    @Override
    public void insertBeaconStatus(String beaconId, int power) {
        deviceMapper.insertBeaconStatus(beaconId, power);
    }

    @Override
    public Room selectRoomBYTagId(String tagId) {
        return deviceMapper.selectRoomBYTagId(tagId);
    }

    @Override
    public void insertRoom(double x, double y, String tagId) {
        deviceMapper.insertRoom(x, y, tagId);
    }

    @DS("location_meta")
    @Override
    public int delBeaconByTs(String beaconId) {
        Beacon beacon = deviceMapper.selectBeaconById(beaconId);
        return deviceMapper.delBeaconByTs(beacon.getTs());
    }

    @Override
    public void insertNoIBeacon(Long ts, String exId, String tagId, int rssi, int tagPower) {
        deviceMapper.insertNoIBeacon(ts, exId, tagId, rssi, tagPower);
    }

    @Override
    public List<NoIbeacon> selectNoIbeacon() {
        return deviceMapper.selectNoIbeacon();
    }

    @Override
    public void saveHighEnergyTag(int energy, int flag, int power, String id) {
        deviceMapper.saveHighEnergyTag(energy,flag,power,id);
    }

}
